//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "GFunctionFactory.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Cashflows/GFunction.h>
#include <gen/QL/Cashflows/CmsCoupon.h>
#include <gen/QL/Quote.h>
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CGFunctionFactory::CGFunctionFactory (boost::shared_ptr<QuantLib::GFunctionFactory>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phGFunctionFactory = NULL;
#endif
	_ppGFunctionFactory = &childNative;
    
}
Cephei::QL::Cashflows::CGFunctionFactory::CGFunctionFactory (QuantLib::GFunctionFactory& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phGFunctionFactory = NULL;
#endif
	_ppGFunctionFactory = new boost::shared_ptr<QuantLib::GFunctionFactory> (&childNative);
    
    _GFunctionFactoryOwner = owner;
    
}

Cephei::QL::Cashflows::CGFunctionFactory::CGFunctionFactory (CGFunctionFactory^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phGFunctionFactory = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppGFunctionFactory = new boost::shared_ptr<QuantLib::GFunctionFactory> (copy->GetShared());
        
    }
}
Cephei::QL::Cashflows::CGFunctionFactory::CGFunctionFactory (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phGFunctionFactory = NULL;
#endif
	if (!t->IsSubclassOf(CGFunctionFactory::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CGFunctionFactory::CGFunctionFactory (QuantLib::Handle<QuantLib::GFunctionFactory>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phGFunctionFactory = &childNative;
	_ppGFunctionFactory = &static_cast<boost::shared_ptr<QuantLib::GFunctionFactory>>(childNative.currentLink());
    
    _GFunctionFactoryOwner = owner;
}
Cephei::QL::Cashflows::CGFunctionFactory::CGFunctionFactory (QuantLib::Handle<QuantLib::GFunctionFactory> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phGFunctionFactory = &childNative;
	_ppGFunctionFactory = &static_cast<boost::shared_ptr<QuantLib::GFunctionFactory>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CGFunctionFactory::CGFunctionFactory (QuantLib::GFunctionFactory childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phGFunctionFactory = NULL;
#endif
	_ppGFunctionFactory = new boost::shared_ptr<QuantLib::GFunctionFactory> (new QuantLib::GFunctionFactory (childNative));
    
}
#endif

Cephei::QL::Cashflows::CGFunctionFactory::~CGFunctionFactory ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppGFunctionFactory != NULL)
    {
	    delete _ppGFunctionFactory;
        _ppGFunctionFactory = NULL;
    }
}
Cephei::QL::Cashflows::CGFunctionFactory::!CGFunctionFactory ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppGFunctionFactory != NULL)
    {
	    delete _ppGFunctionFactory;
    }
}
QuantLib::GFunctionFactory& Cephei::QL::Cashflows::CGFunctionFactory::GetReference ()
{
    if (_ppGFunctionFactory == NULL) throw REFNEW NativeNullException ();
	return **_ppGFunctionFactory;
}
boost::shared_ptr<QuantLib::GFunctionFactory>& Cephei::QL::Cashflows::CGFunctionFactory::GetShared ()
{
    if (_ppGFunctionFactory == NULL) throw REFNEW NativeNullException ();
	return *_ppGFunctionFactory;
}
QuantLib::GFunctionFactory* Cephei::QL::Cashflows::CGFunctionFactory::GetPointer ()
{
    if (_ppGFunctionFactory == NULL) throw REFNEW NativeNullException ();
	return &**_ppGFunctionFactory;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::GFunctionFactory>& Cephei::QL::Cashflows::CGFunctionFactory::GetHandle ()
{
	if (_phGFunctionFactory == NULL)
	{
		_phGFunctionFactory = new Handle<QuantLib::GFunctionFactory> (*_ppGFunctionFactory);
	}
	return *_phGFunctionFactory;
}
#endif
bool Cephei::QL::Cashflows::CGFunctionFactory::HasNative () 
{
	return (_ppGFunctionFactory != NULL);
}

Cephei::QL::Cashflows::IGFunction^ Cephei::QL::Cashflows::CGFunctionFactory::NewGFunctionExactYield (Cephei::QL::Cashflows::ICmsCoupon^ coupon)
{
    CCmsCoupon^ _Ccoupon;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Ccoupon = safe_cast<CCmsCoupon^> (coupon);
        _Ccoupon->Lock();
        QuantLib::CmsCoupon& _coupon = static_cast<QuantLib::CmsCoupon&> (_Ccoupon->GetReference ()); 
    	boost::shared_ptr<QuantLib::GFunction> _rv = (boost::shared_ptr<QuantLib::GFunction>)(*_ppGFunctionFactory)->newGFunctionExactYield ( _coupon );   
        Cephei::QL::Cashflows::CGFunction^ _nrv = REFNEW Cephei::QL::Cashflows::CGFunction (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccoupon != nullptr) _Ccoupon->Unlock();
    }
}
Cephei::QL::Cashflows::IGFunction^ Cephei::QL::Cashflows::CGFunctionFactory::NewGFunctionStandard (UInt64 q, Double delta, UInt64 swapLength)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Size _q = (QuantLib::Size)ValueHelper::Convert (q); //a
        QuantLib::Real _delta = (QuantLib::Real)ValueHelper::Convert (delta); //a
        QuantLib::Size _swapLength = (QuantLib::Size)ValueHelper::Convert (swapLength); //a
    	boost::shared_ptr<QuantLib::GFunction> _rv = (boost::shared_ptr<QuantLib::GFunction>)(*_ppGFunctionFactory)->newGFunctionStandard ( _q,  _delta,  _swapLength );   
        Cephei::QL::Cashflows::CGFunction^ _nrv = REFNEW Cephei::QL::Cashflows::CGFunction (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::IGFunction^ Cephei::QL::Cashflows::CGFunctionFactory::NewGFunctionWithShifts (Cephei::QL::Cashflows::ICmsCoupon^ coupon, Cephei::QL::IQuote^ meanReversion)
{
    CCmsCoupon^ _Ccoupon;
    CQuote^ _CmeanReversion;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Ccoupon = safe_cast<CCmsCoupon^> (coupon);
        _Ccoupon->Lock();
        QuantLib::CmsCoupon& _coupon = static_cast<QuantLib::CmsCoupon&> (_Ccoupon->GetReference ()); 
        _CmeanReversion = safe_cast<CQuote^> (meanReversion);
        _CmeanReversion->Lock();
        Handle<QuantLib::Quote>& _meanReversion = static_cast<Handle<QuantLib::Quote>&> (_CmeanReversion->GetHandle ()); 
    	boost::shared_ptr<QuantLib::GFunction> _rv = (boost::shared_ptr<QuantLib::GFunction>)(*_ppGFunctionFactory)->newGFunctionWithShifts ( _coupon,  _meanReversion );   
        Cephei::QL::Cashflows::CGFunction^ _nrv = REFNEW Cephei::QL::Cashflows::CGFunction (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccoupon != nullptr) _Ccoupon->Unlock();
        if (_CmeanReversion != nullptr) _CmeanReversion->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

